var list = document.getElementById('userName');
var user = localStorage.getItem('user')
var username = JSON.parse(user);
if (username !== null) {
    var li = document.createElement('li');
    var a = document.createElement('a');
    a.innerHTML = 'welcome' + username.data.username;
    li.appendChild(a);
    list.appendChild(li);
}
var bookString = localStorage.getItem('allBooks');
var allBookss = JSON.parse(bookString);
var ip = 'http://192.168.10.102:8888/';
for (var i = 0; i < allBookss.length; i++) {
    var item = document.getElementsByClassName('item')[0];
    var div = document.createElement('div');
    div.className = 'item2';
    // item.appendChild(div);
    var div1 = document.createElement('div');
    div1.className = 'pic';
    div1.innerHTML = '<img src="' + ip + allBookss[i].book__img + '">';
    var div2 = document.createElement('div');
    div2.className = 'infor';
    var h5 = document.createElement('h5');
    h5.innerHTML = allBookss[i].book__name;
    var span1 = document.createElement('span');
    span1.className = 'choose';
    span1.innerHTML = '选服务';
    div2.appendChild(h5);
    div2.appendChild(span1);
    var div3 = document.createElement('div');
    div3.className = 'money1';
    var span2 = document.createElement('span');
    span2.className = 'money';
    span2.innerHTML = '¥' + allBookss[i].book__price + '<br>';
    var span3 = document.createElement('span');
    span3.className = 'cuxiao';
    span3.innerHTML = '促销<i class="fa fa-angle-down" aria-hidden="true"></i>';
    div3.appendChild(span2);
    div3.appendChild(span3);
    var div4 = document.createElement('div');
    div4.className = 'jiajian';
    var span4 = document.createElement('span');
    span4.className = 'jian';
    span4.innerHTML = '<input type="button" value="-" onclick="jian(this)">'
    var span5 = document.createElement('span');
    span5.className = 'mounts';
    span5.innerHTML = allBookss[i].book__number;
    var span6 = document.createElement('span');
    span6.className = 'jia';
    span6.innerHTML = '<input type="button" value="+" onclick="jia(this)">';
    var span7 = document.createElement('span');
    span7.innerHTML = '<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
    var span8 = document.createElement('span');
    span8.innerHTML = '有货';
    span8.className = 'huo';
    div4.appendChild(span4);
    div4.appendChild(span5);
    div4.appendChild(span6);
    div4.appendChild(span7);
    div4.appendChild(span8);
    var div5 = document.createElement('div');
    div5.className = 'allmoney';
    div5.innerHTML = Number(allBookss[i].book__number) * Number(allBookss[i].book__price);
    var div6 = document.createElement('div');
    div6.className = 'del';
    div6.innerHTML = '<input type="button" onclick="dele(this)" value="删除">';
    var sel = document.createElement('div');
    sel.className = 'select'
    sel.innerHTML = '<input type="checkbox" onclick="selectBook(this)" name="chec">'
    item.appendChild(div);
    div.appendChild(sel);
    div.appendChild(div1)
    div.appendChild(div2);
    div.appendChild(div3);
    div.appendChild(div4);
    div.appendChild(div5);
    div.appendChild(div6);
}

var allChecked = document.getElementById('allChecked');
var selec = document.getElementsByName('chec');
var cartNumber = document.getElementById('cartNumber');
var moneyboth = document.getElementById('mon');

// 删除;

var item = document.getElementsByClassName('item')[0];
function dele(ite) {
    for (var i = allBookss.length-1; i >=0 ; i--) {
        // console.log(ite.parentNode.parentNode.children[2].children[0])
        if (ite.parentNode.parentNode.children[2].children[0].innerHTML === allBookss[i].book__name) {
            allBookss.splice(i, 1);
            item.removeChild(item.children[i]);
        }
        
    }
    if(ite.parentNode.parentNode.children[0].children[0].checked){
        cartNumber.innerHTML = Number(cartNumber.innerHTML)-1;
        moneyboth.innerHTML = Number(moneyboth.innerHTML)-Number(ite.parentNode.previousElementSibling.innerHTML); 
    }
    localStorage.setItem('allBooks', JSON.stringify(allBookss));
}
// 增加数量
function jia(btn) {
    for (var i = 0; i < allBookss.length; i++) {
        if (btn.parentNode.parentNode.parentNode.children[2].children[0].innerHTML === allBookss[i].book__name) {
            btn.parentNode.previousElementSibling.innerHTML = Number(allBookss[i].book__number) + 1;
            allBookss[i].book__number++;
            btn.parentNode.parentNode.nextElementSibling.innerHTML = Number(allBookss[i].book__number) * Number(allBookss[i].book__price);
        }
    }
    alll();
    localStorage.setItem('allBooks', JSON.stringify(allBookss));
}
// 减少数量
function jian(btn) {
    for (var i = 0; i < allBookss.length; i++) {
        if (btn.parentNode.parentNode.parentNode.children[2].children[0].innerHTML === allBookss[i].book__name) {
            if (allBookss[i].book__number === 1) {
                btn.parentNode.nextElementSibling.innerHTML = 1;
                allBookss[i].book__number = 1;
                btn.parentNode.parentNode.nextElementSibling.innerHTML = '¥' + Number(allBookss[i].book__number) * Number(allBookss[i].book__price);

            } else {
                btn.parentNode.nextElementSibling.innerHTML = Number(allBookss[i].book__number) - 1;
                allBookss[i].book__number--;
                console.log(Number(allBookss[i].book__number) - 1);
                btn.parentNode.parentNode.nextElementSibling.innerHTML = '¥' + Number(allBookss[i].book__number) * Number(allBookss[i].book__price);
            }
        }
    }
    alll();
    localStorage.setItem('allBooks', JSON.stringify(allBookss));
}
// 单选
function selectBook() {
    var flag = true;
    for (var i = 0; i < selec.length; i++) {
        if (!selec[i].checked) {
            flag = false;
            break;
        }

    }
    alll();
    allChecked.checked = flag;
}
function checkAll(btn) {
    for (var i = 0; i < selec.length; i++) {
        selec[i].checked = allChecked.checked;

    }
    // 全选时商品数量和总金额；
    if (btn.checked) {
        cartNumber.innerHTML = selec.length;
        var aaa = 0;
        for (var i = 0; i < selec.length; i++) {
            aaa += Number(allBookss[i].book__number) * Number(allBookss[i].book__price);
        }
        moneyboth.innerHTML = aaa;
    } else {
        cartNumber.innerHTML = 0;
        moneyboth.innerHTML = 0;
    }
}
// 默认没有商品时 全选不选择
if (selec.length == 0) {
    allChecked.checked = false;
}
// 计算总金额函数
function alll() {
    var a = 0;
    var b = 0;
    for (var i = 0; i < selec.length; i++) {
        if (selec[i].checked) {
            a = Number(a) + Number(allBookss[i].book__number) * Number(allBookss[i].book__price);
            b = Number(b)+1;
        }
    }
    moneyboth.innerHTML = a;
    cartNumber.innerHTML = b;

}
// 结算
function pay() {
    for (var i = selec.length-1; i >=0; i--) {
        if (selec[i].checked) {
            allBookss.splice(i, 1);
            item.removeChild(item.children[i]);
            cartNumber.innerHTML--
        }

    }
    moneyboth.innerHTML = 0;
    localStorage.setItem('allBooks', JSON.stringify(allBookss));
}